package com.magicode.common.service.impl;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.IStaticManagerDao;
import com.magicode.cms.entiy.base.StaticResourceEntity;
//import com.magicode.cms.dao.IStaticHtmlProducerDao;
//import com.magicode.cms.entiy.base.StaticHtmlProducer;
import com.magicode.common.assist.PropertiesUtil;
import com.magicode.common.service.IStaticHtmlService;
import com.magicode.common.util.HtmlGenerator;

@Service("staticHtmlService")
public class StaticHtmlServiceImpl implements IStaticHtmlService {

	@Autowired
	@Qualifier("htmlGenerator")
	private HtmlGenerator generator;
	
	@Autowired
	@Qualifier("daostaticmanager")
	private IStaticManagerDao staticManagerDao;
	
//	@Autowired
//	@Qualifier("staticHtmlProducerDaoImpl")
//	private IStaticHtmlProducerDao staticHtmlProducerDaoImpl;
	
	@Override
	public List<StaticResourceEntity> getGeneratorAutoHtmlUrl() {
		//获取静态化配置表中的url
		String hql = " from " + StaticResourceEntity.class.getName() + " t where t.source=:source and t.valid=1 and t.autoFlag=1 ";
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("source", "mobile");
		List<StaticResourceEntity> list = staticManagerDao.getCommonDaoService().getListBySQLMap(hql, paraMap);
		return list;
	}

	@Override
	public void generatorHtmls() {
		String mainPath = this.getClass().getClassLoader().getResource("/").getPath();
		int num = mainPath.indexOf("WEB-INF");
		mainPath = mainPath.substring(0, num);
		System.out.println("生成静态页面路径："+mainPath);
		String website = PropertiesUtil.parseStr("website");
		Date date = new Date();
		
		String[] names = {"p","main/h","main/v","main/r/reportWaitbroadcast","main/o"};
		/*
		for (String url : names) {
			String name = url;
			if("p".equals(url)){
				name = "index";
			}
			System.out.println("生成静态页面路径："+ mainPath+"html/"+name.replace("/", "_")+".html");
			try {
				generator.createHtmlPage(website+url+"?_refreshHTML="+date.getTime(), mainPath+"html/"+name.replace("/", "_")+".html");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/
//		List<StaticHtmlProducer> list = getStaticHtmlProducerList();
//		for(StaticHtmlProducer staticHtmlProducer : list){
//			String path = staticHtmlProducer.getName();
//			String url = staticHtmlProducer.getAcionUrl();
//			if("p".equals(url)){
//				path = "index";
//			}
//			if(url.indexOf(names.toString()) > -1){
//				System.out.println("生成静态页面路径："+ mainPath+"html/"+path.replace("/", "_")+".html");
//				try {
//					generator.createHtmlPage(website+url+"?_refreshHTML="+date.getTime(), mainPath+"html/"+path.replace("/", "_")+".html");
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
	}
	
	/**
	 * 得到需要静态页面生成实体列表
	 * @return
	 */
//	private List<StaticHtmlProducer> getStaticHtmlProducerList(){
//		String hql = "from " + StaticHtmlProducer.class.getName();
//		try {
//			return staticHtmlProducerDaoImpl.queryDataList(hql, 0, 0);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
	@Override
	public void generatorHtmls(String actionUrl) {
		String mainPath = this.getClass().getClassLoader().getResource("/").getPath();
		String website = PropertiesUtil.parseStr("website");
		website = website + "/";
		int num = mainPath.indexOf("WEB-INF");
		mainPath = mainPath.substring(0, num);
		String[] path = actionUrl.split("/");
		String staticHtmlPath = "";
		Date curDate = new Date();
		Calendar date = Calendar.getInstance(); 
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		for(int i = 0; i < path.length - 1; i ++){
			staticHtmlPath += path[i] + "/";
		}
		staticHtmlPath = staticHtmlPath.trim() + date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH) + 1) + "/" + date.get(Calendar.DAY_OF_MONTH) + "/" + df.format(new Date()) + path[path.length - 1] + ".html";
		System.out.println("生成静态页面路径："+ staticHtmlPath);
		try {
			generator.createHtmlPage(website + actionUrl.trim().substring(1)+"?_refreshHTML="+curDate.getTime(), mainPath + "html" + staticHtmlPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
